package com.Service.Impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.Dao.BaseDaoI;
import com.Entity.DataInfo;
import com.Service.DataInfoService;

@Service("DataInfoService")
public class DataInfoServiceImpl extends BaseServiceImpl<DataInfo> implements
		DataInfoService {

	List<DataInfo> datalist = new ArrayList<DataInfo>();
	List<DataInfo> datalist2 = new ArrayList<DataInfo>();
	DataInfo data = new DataInfo();
	@Autowired
	private BaseDaoI<DataInfo> datadao;

	public List<DataInfo> getLatestDataById(int userid, short datatype) {

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userid);
		params.put("dataType", datatype);

		String hql = "from DataInfo t where t.userId = :userId and t.dataType = :dataType order by t.uploadTime DESC";
		datalist = this.datadao.find(hql, params);
		if (!datalist.isEmpty() && datalist.get(0).getUserId() > 0) {
			return datalist;
		} else {
			return null;
		}
	}

	@Override
	public List<DataInfo> findDataWithOrders(int userId, short datatype,
			int page, int rows) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("dataType", datatype);

		String hql = "from DataInfo t where t.userId = :userId and t.dataType= :dataType "
				+ "order by t.uploadTime DESC";

		// try {
		// datalist=this.datadao.find(hql, params);
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		return datadao.find(hql, params, page, rows);

	}

	@Override
	public Map<String, List> findBloodpressureData(int userId, short datatype,
			int page, int rows) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("dataType", datatype);

		String hql = "from DataInfo t where t.userId = :userId and t.dataType= :dataType "
				+ "order by t.uploadTime DESC";

		Map<String, List> map = new HashMap<String, List>();
		try {
			datalist = this.datadao.find(hql, params, page, rows);
			datatype = 2;
			params.put("dataType", datatype);
			datalist2 = this.datadao.find(hql, params, page, rows);
			// datalist.addAll(datalist2);
			map.put("SBP", datalist);
			map.put("DBP", datalist2);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public List<DataInfo> getDateByDate(int userid, short datatype,
			Date datestart, Date dateover) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userid);
		params.put("dataType", datatype);
		params.put("datestart", datestart);
		params.put("dateover", dateover);

		String hql = "from DataInfo t where t.userId = :userId and t.dataType= :dataType and t.uploadTime >= :datestart and t.uploadTime <= :dateover "
				+ "order by t.uploadTime DESC";

		datalist = this.datadao.find(hql, params);
		if (null != datalist && !datalist.isEmpty()) {
			return datalist;
		} else {
			return null;
		}

	}

}
